package com.qf.smsplatform.controller;

import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.dto.R;
import com.qf.smsplatform.pojo.TAdminUser;
import com.qf.smsplatform.pojo.TDirtyword;
import com.qf.smsplatform.service.TDirtywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dirty")
/*2021/10/26*/public class TDirtywordController {


    private TDirtywordService tDirtywordService;
    @Autowired
    public void settDirtywordService(TDirtywordService tDirtywordService) {
        this.tDirtywordService = tDirtywordService;
    }

    @RequestMapping("/selectOne")
    public R findDirtyById(Long id){
        TDirtyword dirtyById = tDirtywordService.findDirtyById(id);
        return R.setOk(dirtyById);
    }
    @RequestMapping("/add")
    public R addDirtyword(String dirtyword, HttpSession session){
        //从登录用户中获取信息
        TAdminUser user = (TAdminUser)session.getAttribute("user");
        TDirtyword tDirtyword=new TDirtyword();
        if(user!=null){
            tDirtyword.setCreater(user.getId());
            tDirtyword.setDirtyword(dirtyword);
            tDirtyword.setOwntype(user.getType());
        }
        //调用service进行黑名单数据的插入
        tDirtywordService.addDirtyWord(tDirtyword);
        return R.setOk();
    }

    @RequestMapping("/selectPage")
    public R selectPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){

        PageInfo<TDirtyword> pageInfo = tDirtywordService.selectPage(pageNum, pageSize);
        return R.setOk(pageInfo);
    }

    @RequestMapping("/delete")
    public R deleteByIds(@RequestBody Map map){
        Integer integer = tDirtywordService.deleteByIds((List<Integer>)map.get("ids"));
        if(integer>0){
            return R.setOk();
        }
        return R.setError();
    }
    @PostMapping("/update")
    public R updateDirtyWord(String dirtyWord,Long id,HttpSession session){
        //从session中获取当前登录用户的信息
        TAdminUser user = (TAdminUser)session.getAttribute("user");
        //先查询当前数据是由谁创建的
        TDirtyword dirtyById = tDirtywordService.findDirtyById(id);
        //封装为对象，调用业务层进行数据的修改，相关判断交给业务层进行
        TDirtyword tDirtyword=new TDirtyword();
        if(user!=null){
            tDirtyword.setDirtyword(dirtyWord);
            tDirtyword.setOwntype(dirtyById.getOwntype());
            tDirtyword.setCreater(dirtyById.getCreater());
            tDirtyword.setId(id);
        }
        tDirtywordService.updateTDirtyWord(tDirtyword);
        return R.setOk();
    }
}
